package team3;

import project2.AI.Player;

/**Primary GUI. Provides an interface for starting games, and hosts the Panel showing Game State.
 * <p>
 * Note: only one game at a time is allowed to run. The Start Button is greyed
 * out on game start. This prevents possible system resource issues from having, say,
 * 3 games of hard AI vs hard AI running in the background.
 */
public class ReversiGUI extends javax.swing.JFrame implements GameListener
{
    /** Builds a new Reversi GUI.
     */
    public ReversiGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        blackLabel = new javax.swing.JLabel();
        blackSelector = new javax.swing.JComboBox();
        whiteLabel = new javax.swing.JLabel();
        whiteSelector = new javax.swing.JComboBox();
        startGameButton = new javax.swing.JButton();
        showMoves = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSplitPane1.setDividerLocation(100);

        blackLabel.setText("Black Player");

        blackSelector.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "HUMAN", "EASY", "MEDIUM", "HARD", "LEARNING", "RANDOM" }));

        whiteLabel.setText("White Player");

        whiteSelector.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "HUMAN", "EASY", "MEDIUM", "HARD", "LEARNING", "RANDOM" }));

        startGameButton.setText("Start Game");
        startGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startGame(evt);
            }
        });

        showMoves.setText("Show Moves");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(blackSelector, 0, 99, Short.MAX_VALUE)
            .addComponent(whiteSelector, 0, 99, Short.MAX_VALUE)
            .addComponent(showMoves, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
            .addComponent(startGameButton, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(blackLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(whiteLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(blackLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(blackSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(whiteLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(whiteSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(showMoves)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(startGameButton)
                .addContainerGap(185, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 396, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 328, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Handles the 'startGame' button being clicked. Creates the desired AIs,
     * kicks off a game manager, creates a ReversiPanel and adds it to the list,
     * and finally, disables the start button.
     */
	private void startGame(java.awt.event.ActionEvent evt)//GEN-FIRST:event_startGame
	{//GEN-HEADEREND:event_startGame
		startGameButton.setEnabled(false);
		project2.AI black = getAI(blackSelector.getSelectedItem().toString(), AI.Player.BLACK);
		project2.AI white = getAI(whiteSelector.getSelectedItem().toString(), AI.Player.WHITE);
		GameManager manager = new GameManager(black, white);
		ReversiPanel panel=new ReversiPanel(manager, this, showMoves.isSelected());
		this.jSplitPane1.setRightComponent(panel);
        manager.registerGameListener(this);
		manager.startGameLoop();
	}//GEN-LAST:event_startGame

    /** Builds an AI based on the name input, and the color it should be.
     * @param aiName The AI's name, as from the ComboBoxes.
     * @param color The AI's color
     * @return An AI object of the given color.
     */
	private project2.AI getAI(String aiName, AI.Player color)
	{
		if(aiName.equalsIgnoreCase("HUMAN"))
			return new Human(color);
		else if(aiName.equalsIgnoreCase("EASY"))
			return new AI(color, AI.Difficulty.EASY);
		else if(aiName.equalsIgnoreCase("MEDIUM"))
			return new AI(color, AI.Difficulty.MEDIUM);
		else if(aiName.equalsIgnoreCase("HARD"))
			return new AI(color, AI.Difficulty.HARD);
        else if(aiName.equalsIgnoreCase("LEARNING"))
    		return new LearningAI(color);
        return new RandomAI(color);
	}

    /** Entry point for the Game
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ReversiGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel blackLabel;
    private javax.swing.JComboBox blackSelector;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JCheckBox showMoves;
    private javax.swing.JButton startGameButton;
    private javax.swing.JLabel whiteLabel;
    private javax.swing.JComboBox whiteSelector;
    // End of variables declaration//GEN-END:variables

    /** Unused handler from Game Listener */
	public void nextPlayersTurn(GameManager game, Player currentPlayer, int[] lastMove)
	{}

    /**Handler for gameOver, signals that the start button can be re-enabled
     */
	public void gameOver(GameManager game, Player winner)
	{
		this.startGameButton.setEnabled(true);
	}
}
